package android.fwirk.plurk;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class PlurkUser {
	int friendsCount;
	int fansCount;
	int unreadCount;
	int alertsCount;
	int userID;
	String nickName;
	String displayName;
	int hasProfileImage;
	String avatar;
	String location;
	Date dateOfBirth;
	String fullName;
	String gender;
	String pageTitle;
	double karma;
	int recruited;
	String relationship;
	String pic=null;
	String apic=null;
	Bitmap picture=null;

	public PlurkUser(JSONObject data) {
		parse(data);
	}

	public void parse(JSONObject data) {
		friendsCount = data.optInt("friends_count");
		fansCount = data.optInt("fans_count");
		unreadCount = data.optInt("unread_count");
		alertsCount = data.optInt("alerts_count");
		if (!data.isNull("user_info")) {
			data = data.optJSONObject("user_info");
		}
		if (data != null) {
			userID = data.optInt("id");
			nickName = data.optString("nick_name");
			if (!data.isNull("display_name")) {
				displayName = data.optString("display_name");
			} else {
				displayName = "";
			}
			hasProfileImage = data.optInt("has_profile_image");
			if (!data.isNull("avatar")) {
				avatar = data.optInt("avatar") + "";
			} else {
				avatar = null;
			}
			if (hasProfileImage == 1) {
				pic = "http://avatars.plurk.com/" + userID + "-medium.gif";
			}
			if (hasProfileImage == 1 && avatar != null) {
				apic = "http://avatars.plurk.com/" + userID + "-medium" + avatar
						+ ".gif";
			}
			location = data.optString("location");
			dateOfBirth = new Date(data.optString("date_of_birth"));
			fullName = data.optString("full_name");
			gender = (data.optInt("gender") == 1 ? "Male" : "Female");
			karma = data.optDouble("karma");
			recruited = data.optInt("recruited");
			relationship = data.optString("relationship");
		}
	}

	@SuppressWarnings("unchecked")
	public static List<PlurkUser> getUsers(Context context, JSONObject users) {
		List<PlurkUser> list = null;
		if (users.length() > 0) {
			list = new LinkedList<PlurkUser>();
		}
		PlurkUser p;
		Iterator ids = users.keys();
		while (ids.hasNext()) {
			String id = (String) ids.next();
			try {
				JSONObject u = users.getJSONObject(id);
				p = new PlurkUser(u);
				list.add(p);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static PlurkUser getUser(List<PlurkUser> list, int id) {
		for (PlurkUser p : list) {
			if (p.getUserID() == id) {
				return p;
			}
		}
		return null;
	}

	public int getFriendsCount() {
		return friendsCount;
	}

	public void setFriendsCount(int friendsCount) {
		this.friendsCount = friendsCount;
	}

	public int getFansCount() {
		return fansCount;
	}

	public void setFansCount(int fansCount) {
		this.fansCount = fansCount;
	}

	public int getUnreadCount() {
		return unreadCount;
	}

	public void setUnreadCount(int unreadCount) {
		this.unreadCount = unreadCount;
	}

	public int getAlertsCount() {
		return alertsCount;
	}

	public void setAlertsCount(int alertsCount) {
		this.alertsCount = alertsCount;
	}

	public int getUserID() {
		return userID;
	}

	public void setUserID(int userID) {
		this.userID = userID;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public int getHasProfileImage() {
		return hasProfileImage;
	}

	public void setHasProfileImage(int hasProfileImage) {
		this.hasProfileImage = hasProfileImage;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public Date getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getPageTitle() {
		return pageTitle;
	}

	public void setPageTitle(String pageTitle) {
		this.pageTitle = pageTitle;
	}

	public double getKarma() {
		return karma;
	}

	public void setKarma(double karma) {
		this.karma = karma;
	}

	public int getRecruited() {
		return recruited;
	}

	public void setRecruited(int recruited) {
		this.recruited = recruited;
	}

	public String getRelationship() {
		return relationship;
	}

	public void setRelationship(String relationship) {
		this.relationship = relationship;
	}

	public String getPic() {
		return pic;
	}

	public void setPic(String pic) {
		this.pic = pic;
	}

	public Bitmap getPicture() {
		return picture;
	}

	public void setPicture(Bitmap picture) {
		this.picture = picture;
	}

	public void downloadPic() {
		if (hasProfileImage == 1 && avatar != null) {
			picture = getImage(apic);
		}
		if (picture==null && hasProfileImage == 1) {
			picture = getImage(pic);
		}
		
		
	}

	public Bitmap getImage(String address) {
		try {
			URL url = new URL(address);
			Object content = url.getContent();
			InputStream is = (InputStream) content;
			Bitmap b = BitmapFactory.decodeStream(is);
			is.close();
			return b;
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
}
